<?php
namespace App\Models;

class Card extends BaseModel
{
    protected $table = "card";

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }

    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    /**
     * 获取卡内余额
     */
    public function getCardRes($request)
    {
        $card = $this->getOne(['member_id'=>$request['m_id']]);
        $data['money'] = 0;
        if ($card) {
            $res = apiCurl('memberAccountQuery', $card['card_no']);
            $data['money'] = $res['基本余额'];
        }
        return $data;
    }

    /**
     * 获取我的就诊卡列表
     *
     */
    public function getMyCard($request)
    {
        $member = new Member();
        $where['user_id'] = $request['mid'];
        $myMemberList = $member->getList($where);
        $myMemberIds = [];
        foreach ($myMemberList as $v) {
            $myMemberIds [] = $v['id'];
        }
        $c_where['user_id'] = $request['mid'];
        $c_where['member_id'] = array('in', $myMemberIds);
        $order['id'] = 'desc';
        $cardList = $this->getList($c_where, "*", $order, 10);
        if ($cardList) {
            $cardList = $cardList->toArray();
            $cardList['pageNoList'] = getPageNoList($cardList['last_page'], request('page', 1), 10);
        }
        return $cardList;
    }

    /**
     *
     * 绑定就诊卡
     */

    public function  bindCard($request){

          $data['user_id'] = $request['mid'];
          $data['member_id'] =  $request['m_id'];
          $member = new Member();
          $user = new User();
          $Sms = new SmsLog();
          $res = $Sms->checkSmsCaptcha($request);
          if($res['state']){
                return $res;
          }
          $member_info = $member->getOne(array('id'=>$request['m_id']));
          if(empty($member_info)){
               return ['state'=>0,'msg'=>'就诊人参数错误'];
          }

          $member_info = $member_info->toArray();
          $data['name'] = $member_info['name'];
          $data['card_no'] = $request['card_no'];
          $data['status'] = 1;
          $data['created_at'] = date('Y-m-d H:i:s',time());

          // 判断改卡是否在数据库
          $card_res = $this->getOne(array('card_no'=>$data['card_no'],"status"=>1));
          if($card_res){
                return ['state'=>0,'msg'=>'该卡已被绑定'];
          }
          $res = apiCurl('memberRelation',array('cards'=>$request['card_no'],'name'=>$member_info['name'],'phoneno'=>$member_info['mobile']));
          if(isset($res[0])){
              $newarr = [];
              foreach($res as $v){
                  if($v['VAA05']==$data['name']&&$v['VCB04']==$data['card_no']){
                      $newarr = $v;
                  }
              }
          }else{
              $newarr = $res;
          }
          if(isset($newarr['VCB04'])){
              $card_type = $newarr['cardcategory'];
          }else {
              if ($newarr['rtn'] == 2) {
                  return ['state' => 0, 'msg' => '该卡号不存在'];
              }
              if ($newarr['rtn'] == 3) {
                  return ['state' => 0, 'msg' => '卡号对应的姓名与传入的不相符'];
              }
              if ($newarr['rtn'] == 4) {
                  return ['state' => 0, 'msg' => '卡号对应的手机号与传入的不相符'];
              }
              if ($newarr['rtn'] == 5) {
                  return ['state' => 0, 'msg' => '卡号已失效'];
              }
              if ($newarr['rtn'] == 6) {
                  return ['state' => 0, 'msg' => '接口内部错误'];
              }
          }

          $updata['three_card_no'] = '0';
          // 主卡
          if($newarr['VBU08']==6){
              if($card_type==2){
                  $updata['primary_card_no'] = $data['card_no'];
              }else if($card_type==3){
                  $updata['second_card_no'] = $data['card_no'];
              }
          }
          $uwhere['id'] = $request['mid'];
          $u_res = $user->up($uwhere,$updata);
          $his_res = apiCurl('cardBinding',array('cardno'=>$request['card_no'],'tel'=>$member_info['mobile'],'id_card'=>'','type'=>0));
         if(isset($his_res['ret'])){
             return ['state'=>0,'msg'=>'该就诊卡不存在'];
         }
          if($his_res) {
              $data['reffer_id'] = $his_res['VAA01'];
              $data['type'] = $newarr['cardcategory'];
              $data['level'] = $newarr['VBU08'];
              $res = $this->add($data);
          }
          if(!$u_res&&$res){
              return ['state'=>0,'msg'=>'绑定就诊卡失败'];
          }
          return ['state'=>1,'msg'=>'绑定就诊卡成功'];
    }

    /**
     * 绑定卡号
     */
    public function his_card_binding($param)
    {
        $data['cardno'] = $param['cardno'];
        $data['tel'] = $param['tel'];
        $data['id'] = $param['id_card'];
        $data['type'] = $param['type'];
        return $data;
    }

    public function losscard($request)
    {
        $res = apiCurl('memberLossReport', $request['card_no']);
        $where['card_no'] = $request['card_no'];
        $where['status'] = 1;
        $updata['status'] = 2;
        $this->up($where, $updata);
        return $res;
    }

    /**
     * 获取卡号余额
     */
    public function his_member_account_query($cardno)
    {
        $data['cardno'] = $cardno;
        return $data;
    }

    /**
     * 获取卡号信息
     */
    public function his_member_relation($params)
    {
        $data['cardno'] = $params['cards'];
        $data['name'] = $params['name'];
        $data['phoneno'] = $params['phoneno'];
        return $data;
    }

    /**
     * 会员卡挂失
     * @param $cardno string 会员卡
     * @return array
     * 1 成功 0 失败
     */
    public function his_member_loss_report($card)
    {
        $data['cardno'] = $card;
        return $data;
    }

    /**
     * 锁定卡号
     * @param $sca01    就诊人登记id
     * @param $amounts  金额
     *
     * @return
     * rtn 1 成功
     * msg 提示信息
     * cardno 卡号
     */
    public function his_lock_card_no($param)
    {
        $data['sca01'] = $param['sca01'];
        $data['amounts'] = $param['amounts'];
        return $data;
    }

    /**
     * 发卡并授权给用户
     * @param $sca01   就诊人登记id
     * @param $cardno  锁定卡号
     * @param $amounts 充值金额
     * @param $billno  充值单号
     * @param $wxbillno 微信官方支付单号
     * @param $wxpaytime 支付时间  微信官方支付时间  y-m-d H:i:s
     * @param $wxopenid  用户OpenID  用户OpenID   相对本公众号唯一
     *
     * @return
     *  1、Result    1-成功
    2、errmsg    提示信息
    3、vaa01     HIS病人ID
     */
    public function his_grant_card($param)
    {
        $data['sca01'] = $param['sca01'];
        $data['cardno'] = $param['cardno'];
        $data['amounts'] = $param['amounts'];
        $data['billno'] = $param['billno'];
        $data['wxbillno'] = $param['wxbillno'];
        $data['wxpaytime'] = $param['wxpaytime'];
        $data['wxopenid'] = $param['wxopenid'];
        return $data;
    }

}